local DsPlayer = FindMetaTable("Player")

local MIN_BALL_KICK_STRENGTH = 650
local MAX_BALL_KICK_STRENGTH = 1500
local SWING_TIME = 2

function DsPlayer:BallReceiving()
	if self.IsSwing then
		self:KickBall()
	end
end

function DsPlayer:IsBallHolder()
	return self:IsBallOwner() and Ball.IsUnderControl()
end

function DsPlayer:IsBallOwner()
	return Ball.GetOwner() == self
end

function DsPlayer:StartSwing()
	self.IsSwing = true
	self:SetStartSwingTime(tonumber(os.clock()))
	if self:IsBallHolder() then
		timer.Create("Swing timer"..self:GetName(), SWING_TIME, 1, function()
				self:KickBall()
			end)
	end
end

function DsPlayer:EndSwing()
	self.IsSwing = false
	if self:IsBallHolder() then
		self:KickBall()
	end
end

function DsPlayer:KickBall()
	local strengthMultiplier = self:GetSwingDeltaTime() / SWING_TIME
	percent = 100 * strengthMultiplier
	local maxDeltaStrength = MAX_BALL_KICK_STRENGTH - MIN_BALL_KICK_STRENGTH
	local strength = MIN_BALL_KICK_STRENGTH + (maxDeltaStrength * strengthMultiplier)
	self:ChatPrint("kick: "..tostring(percent).."% ("..tostring(strength)..")")
	Ball.Kick(strength)
	timer.Destroy("Swing timer"..self:GetName())
end

function DsPlayer:SetStartSwingTime(time)
	self.StartSwingTime = time
end

function DsPlayer:GetSwingDeltaTime()
	local deltaTime = tonumber(os.clock()) - self.StartSwingTime
	if deltaTime <= SWING_TIME then
		return deltaTime
	else
		return SWING_TIME
	end
end